package com.senpai.ikhtiar;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.senpai.database.DatabaseHelper;
import com.senpai.database.Question;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class QuestionActivity extends ActionBarActivity implements
		OnClickListener {

	int number_question = 0;
	String myJsonQuestion;
	TextView id_q, category_q, question_q;
	int idQuestion;
	String category, question, formattedDate;
	
	DatabaseHelper database = new DatabaseHelper(this);

	public static String NUMBER = "Id_Question";
	public static String CATEGORY = "Category_Question";
	public static String QUESTION = "Content_Question";

	Button dac, nonDac, dontKnow;
	DateFormat dateFormatter;
	Date today;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_question);

		dateFormatter = new SimpleDateFormat("dd-MM-yyyy HH:mm");
		dateFormatter.setLenient(false);
		today = new Date();
		formattedDate = dateFormatter.format(today);

		dac = (Button) findViewById(R.id.ok_button);
		nonDac = (Button) findViewById(R.id.no_button);
		dontKnow = (Button) findViewById(R.id.dont_button);
		dac.setOnClickListener(this);
		nonDac.setOnClickListener(this);
		dontKnow.setOnClickListener(this);
		getQuestion();
	}

	private void getQuestion() {

		id_q = (TextView) findViewById(R.id.questionNumber);
		category_q = (TextView) findViewById(R.id.questionCategory);
		question_q = (TextView) findViewById(R.id.question);

		StringBuffer sb = new StringBuffer();
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(getAssets().open(
					"QuestionsData.json")));
			String temp;
			while ((temp = br.readLine()) != null)
				sb.append(temp);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		myJsonQuestion = sb.toString();

		try {

			JSONObject jsonObjMain = new JSONObject(myJsonQuestion);
			JSONArray jsonArray = jsonObjMain.getJSONArray("Questions");
			JSONObject jsonObj = jsonArray.getJSONObject(number_question);

			idQuestion = jsonObj.getInt("Id_Question");
			category = jsonObj.getString("Category_Question");
			question = jsonObj.getString("Content_Question");

			id_q.setText(Integer.toString(idQuestion));
			category_q.setText(category);
			question_q.setText(question);

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		getMenuInflater().inflate(R.menu.question, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ok_button:
			if (number_question == 29) {
				Intent intent = new Intent(QuestionActivity.this,
						TestActivity.class);
				startActivity(intent);
			} else {
				number_question++;
				getQuestion();
				database.addQuestion(new Question(idQuestion, "1", question,
						category, formattedDate));
			}
			break;
		case R.id.no_button:
			if (number_question == 29) {
				Intent intent = new Intent(QuestionActivity.this,
						TestActivity.class);
				startActivity(intent);
			} else {
				number_question++;
				getQuestion();
				database.addQuestion(new Question(idQuestion, "2", question,
						category, formattedDate));
			}
			break;
		case R.id.dont_button:
			if (number_question == 29) {
				Intent intent = new Intent(QuestionActivity.this,
						TestActivity.class);
				startActivity(intent);
			} else {
				number_question++;
				getQuestion();
				database.addQuestion(new Question(idQuestion, "3", question,
						category, formattedDate));
			}
			break;
		}

	}
}
